Hệ thống quản lý hàng tồn kho bằng Python

1 #Inventory Management System 2
2 #SaurontheMighty
3
4
5 #Dictionaries
6 unit_price={}
7 description={}
8 stock={}
9
10 #Open file with stock
11 details = open(
"stock.txt","r")
12
13 #First line of the file
is the number of items
14 no_items =
int((details.readline()).rstrip("\n"))
15
16 #Add items to dictionaries

17 for
i in range(0,no_items):
18     line = (details.readline()).rstrip(
"\n")
19     x1,x2 = line.split(
"#")
20     x1=
int(x1)
21     x2=
float(x2)
22     unit_price.update({x1: x2})

23
24 for
i in range(0,no_items):
25     line = (details.readline()).rstrip(
"\n")
26     x1,x2 = line.split(
"#")
27     x1=
int(x1)
28     description.update({x1: x2})

29
30 for
i in range(0,no_items):
31     line = (details.readline()).rstrip(
"\n")
32     x1,x2 = line.split(
"#")
33     x1=
int(x1)
34     x2=
int(x2)
35     stock.update({x1: x2})
36
37 details.close()
38
39 #List to store the items purchased
40 cart=[]
41
42 c=
"y" #Runs the while loop as long as user wants
43
44
45 #Instructions
46 print(
"Welcome to IMS")
47 print()
48 print(
"A-Add an item")
49 print(
"R-Remove an item")
50 print(
"E-Edit specifics of an item")
51 print(
"L-List all items")
52 print(
"I-Inquire about a part")
53 print(
"P-Purchase")
54 print(
"C-Checkout")
55 print(
"S-Show all parts purchased")
56 print(
"Q-Quit")
57 print(
"remove-Remove an item from the cart")
58 print(
"help-See all commands again")
59 print()
60
61
62 total_cost=
0
63 flag=
0 #To check if they have checked out
64
65
66 while
(c!= "q" or c!= "Q"):
67     c= input(
"What would you like to do? ")
68     
69     
if(c=="q" or c=="Q"):
70         
break
71         
72     elif(c==
"A" or c=="a"):#Add a part
73         p_no =
int(input("Enter part number: "))
74         p_pr =
float(input("Enter part price: "))
75         p_desc = input(
"Enter part description: ")
76         p_stock =
int(input("Enter part stock: "))
77         
78         m=
0
79         
for i in range(0,len(unit_price)):
80             
if(p_no in unit_price):
81                 p_no+=
1
82                 m=
1
83         
if(m==1):
84             print()
85             print(
"That part number already exists :(, changing value to ",p_no)
86                 
87         unit_price.update({p_no: p_pr})
88         description.update({p_no: p_desc})
89         
if(p_stock > -1):
90             stock.update({p_no: p_stock})
91         
else:
92             p_stock =
0
93             stock.update({p_no: p_stock})
94             print(
"The stock of an item cannot be negative, the stock has been set to 0.")
95         print()
96         print(
"Part number: ",p_no," Description: ",description.get(p_no)," Price: ",unit_price.get(p_no)," Stock: ",stock.get(p_no))
97         print(
"Part was added successfully!")
98         print()
99         
100     elif(c==
"E" or c=="e"):#Edit a part
101         print()
102         p_no =
int(input("Enter part number: "))
103         
if(p_no in unit_price):
104             p_pr =
float(input("Enter part price: "))
105             p_desc = input(
"Enter part description: ")
106             p_stock =
int(input("Enter part stock: "))
107                 
108             unit_price.update({p_no: p_pr})
109             description.update({p_no: p_desc})
110             stock.update({p_no: p_stock})
111             
112         
else:
113             print(
"That item does not exist, to add an item use a")
114         print()
115     
116             
117     elif(c==
"R" or c=="r"):#Remove a part
118         print()
119         p_no =
int(input("Enter part number: "))
120         
if(p_no in unit_price):
121             are_you_sure = input(
"Are you sure you want to remove that item(y/n)? ")
122             
if(are_you_sure=="y" or are_you_sure=="Y"):
123                 unit_price.pop(p_no)
124                 description.pop(p_no)
125                 stock.pop(p_no)
126                 print(
"Item successfully removed!")
127             print()
128         
else:
129             print(
"Sorry, we don't have such an item!")
130             print()
131         
132     elif(c==
"L" or c=="l"):#List all the parts
133         print()
134         print(
"Parts and their prices: ",unit_price)
135         print(
"Descriptions: ",description)
136         print(
"Stock left of Item: ",stock)
137         print()
138
139     elif(c==
"I" or c=="i"):#Inquire about a part
140         print()
141         p_no=
int(input("Enter Part Number: "))
142         
if(p_no in unit_price):
143             print()
144             print(
"Part number: ",p_no," Description: ",description.get(p_no)," Price: ",unit_price.get(p_no)," Stock: ",stock.get(p_no))
145             
if(stock.get(p_no)<3 and stock.get(p_no)!=0):
146                 print(
"Only ",stock.get(p_no)," remaining! Hurry!")
147             print()
148         
else:
149             print(
"Sorry we don't have such an item!")
150             print()
151         
152     elif(c==
"P" or c=="p"):#Purchase a part
153         print()
154         p_no =
int(input("Enter Part number: "))
155         
if(p_no in unit_price):
156             
if(flag==1):
157                 flag=
0
158             stock_current = stock.
get(p_no)
159             
if(stock_current>0):
160                 stock_current = stock.
get(p_no)
161                 stock[p_no] = stock_current-
1
162                 item_price = unit_price.
get(p_no)
163                 total_cost = total_cost+item_price
164                 print(description.
get(p_no),"added to cart: ","$",item_price)
165                 cart.append(p_no)#Stores item
in cart
166             
else:
167                 print(
"Sorry! We don't have that item in stock!")
168         
else:
169                 print(
"Sorry! We don't have such an item!")
170         print()
171         
172     elif(c==
"C" or c=="c"):#Check out
173         print()
174         print(
"You bought the following parts: ",cart)
175         print(
"Total: ","$",round(total_cost,2))
176         tax= round(
0.13*total_cost,2)
177         print(
"Tax is 13%: ","$",tax)
178         total = round(total_cost+tax,
2)
179         print(
"After Tax: ","$",total)
180         total_cost=
0
181         flag=
1
182         print()
183         print(
"You can still purchase items after check out, your cart has been reset. To quit press q")
184         print()
185         
186     elif(c==
"help"):#Display all commands
187         print()
188         print(
"Help Centre")
189         print(
"A-Add an item")
190         print(
"R-Remove an item")
191         print(
"E-Edit specifics of an item")
192         print(
"L-List all items")
193         print(
"I-Inquire about a part")
194         print(
"P-Purchase")
195         print(
"C-Checkout")
196         print(
"S-Show all parts purchased")
197         print(
"remove-Remove an item from the cart")
198         print(
"help-See all commands again")
199         print(
"If you have any other questions or concerns please contact the manager.")
200         print()
201         
202     elif(c==
"remove" or c=="Remove"):#To remove an item from the cart
203         print()
204         are_you_sure = input(
"Are you sure you want to remove an item from the cart(y/n)? ")
205         
if(are_you_sure=="y"):
206             p_no =
int(input("Enter part number to remove from cart: "))
207             
if(p_no in cart):
208                 stock_current = stock.
get(p_no)
209                 stock[p_no] = stock_current+
1
210                 item_price = unit_price.
get(p_no)
211                 total_cost = total_cost-item_price
212                 j=
0
213                 
for i in range(0,len(cart)):#To find the index of the part in the list cart
214                     
if(i==p_no):
215                         j=i
216
217                 cart.pop(j)
218                 print(description.
get(p_no),"removed from cart: ")
219                 print()
220             
else:
221                 print()
222                 print(
"That item is not in your cart!")
223                 print()
224                 
225     elif(c==
"s" or c=="S"):#prints list cart
226         print()
227         print(cart)
228         print()
229         
230     
else:
231         print()
232         print(
"ERROR! Contact manager for help!")
233         print()
234
235
236 #Outputs total
if the user quits without checking out
237 if
(total_cost>0 and flag==0):
238     print()
239     print(
"You bought: ",cart)
240     print(
"Total: ","$",round(total_cost,2))
241     tax= round(
0.13*total_cost,2)
242     print(
"Tax is 13%: ","$",tax)
243     total = round(total_cost+tax,
2)
244     print(
"After Tax: ","$",total)
245     
246 print()
247 print(
"Thank you for using IMS")
248
249 #Write the updated inventory to the file
250 details = open(
"stock.txt","w")
251 no_items=len(unit_price)
252 details.write(str(no_items)+
"\n")
253 for
i in range(0,no_items):
254     details.write(str(i+
1)+"#"+str(unit_price[i+1])+"\n")
255     

256 for
i in range(0,no_items):
257     details.write(str(i+
1)+"#"+description[i+1]+"\n")
258     

259 for
i in range(0,no_items):
260     details.write(str(i+
1)+"#"+str(stock[i+1])+"\n")
261     
262 details.close()


Gõ tìm kiếm nhanh...